package com.geek.bucks;

import com.geek.bucks.entity.VO;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.WebRequest;

@ControllerAdvice
public class ExceptionHandlerAdvice {

    @ExceptionHandler(value = {Exception.class})
    public VO<Object> exception(Exception exception, WebRequest webRequest) {
        int code = 20000;
        if (exception instanceof IllegalArgumentException) {
            code = 20001;
        }
        return VO.error(code, "参数错误：" + exception.getCause());
    }

    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) {
        webDataBinder.setDisallowedFields("id");
    }
}
